﻿Option Strict On
Option Explicit On

Imports System.Text
Imports System.Xml
Imports System.Xml.Serialization

Public Class Project
   Public Sub New()
      'required for serialization
   End Sub

   Public Sub New(ByVal collectionUri As Uri, ByVal projectName As String, ByVal processTemplateName As String)
      If collectionUri Is Nothing Then
         Throw New ArgumentNullException("collectionUri")
      Else
         Me.TFSName = collectionUri.AbsoluteUri
      End If

      If String.IsNullOrWhiteSpace(projectName) Then
         Throw New ArgumentNullException("projectName")
      Else
         Me.ProjectName = projectName.Trim()
      End If

      If String.IsNullOrWhiteSpace(processTemplateName) Then
         Throw New ArgumentNullException("processTemplateName")
      Else
         Me.ProcessTemplateName = processTemplateName.Trim()
      End If


      'Set default project attributes
      Me.LogFolder = String.Empty

      Me.ProjectSiteEnabledSpecified = True
      Me.ProjectSiteEnabled = trueFalse.False
      Me.ProjectSiteWebApplication = String.Empty
      Me.ProjectSitePath = String.Empty
      Me.ProjectSiteTitle = String.Empty
      Me.ProjectSiteDescription = String.Empty

      Me.sccCreateTypeFieldSpecified = True
      Me.SccCreateType = sccCreateTypeEnum.None

      Me.AddFeaturesToExistingProjectSpecified = True
      Me.AddFeaturesToExistingProject = trueFalse.False

      Me.ProjectReportsEnabledSpecified = True
      Me.ProjectReportsEnabled = trueFalse.False
      Me.ProjectReportsForceUploadSpecified = True
      Me.ProjectReportsForceUpload = trueFalse.False
   End Sub

   Public Function ToXML() As String
      'init XML serialization
      Dim sb As New StringBuilder()
      Dim settings As New XmlWriterSettings()
      With settings
         .Encoding = Encoding.UTF8
         .Indent = True
         .IndentChars = New String(" "c, 2)
         .OmitXmlDeclaration = True  'bug: even though UTF8 specified, declaration is always UTF-16 (which VS won't process!); get rid of it...
      End With
      Dim xmlWriter As XmlWriter = xmlWriter.Create(sb, settings)

      'serialize the objects
      Dim ser As New XmlSerializer(Me.[GetType]())
      ser.Serialize(xmlWriter, Me)
      xmlWriter.Close()

      Return sb.ToString
   End Function
End Class
